import json
import os

father_path = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".")


# {"code":{""}}
def select_doc():
    file_names = {}
    path = father_path
    if len(path.split('\\')) == 3:
        path = father_path + "\\stock"
    for root, dirs, files in os.walk(path + "\\config"):
        for file in files:
            infos = file.split('.')
            file_names[infos[0]] = file
    print(file_names)
    deal_doc_flag = input('选择文件序号***(默认3.自选股票):')
    if deal_doc_flag == '':
        deal_doc_flag = '3'
    # 文件
    stock_file = open(path + '\\' + 'config' + '\\' + file_names[deal_doc_flag], mode='r', encoding='utf-8')
    doc = {}
    while 1:
        line = stock_file.readline()[:-1]
        if line == '':
            break
        line_j = json.loads(line.replace("\'", "\""))
        doc[line_j['code']] = line_j
    stock_file.close()
    return doc


# {"code":{""}}
def select_doc_path(path):
    file_names = {}
    for root, dirs, files in os.walk(path):
        for file in files:
            infos = file.split('.')
            file_names[infos[0]] = file
    print(file_names)
    deal_doc_flag = input('选择文件序号***(默认0):')
    if deal_doc_flag == '':
        deal_doc_flag = '0'
    # 文件
    stock_file = open(path + '\\' + file_names[deal_doc_flag], mode='r', encoding='utf-8')
    doc = {}
    while 1:
        line = stock_file.readline()[:-1]
        if line == '':
            break
        line_j = json.loads(line.replace("\'", "\""))
        doc[line_j['code']] = line_j
    stock_file.close()
    return doc


def select_doc_name(path):
    file_names = {}
    for root, dirs, files in os.walk(path):
        for file in files:
            infos = file.split('.')
            file_names[infos[0]] = file
    count = 0
    name_list = ''
    for name in file_names:
        count += 1
        name_list += (file_names[name] + "\t\t")
        if count == 6:
            print(name_list)
            name_list = ''
            count = 0
    print(name_list)
    deal_doc_flag = input('选择文件序号***(重新生成00):')
    if deal_doc_flag == '' or deal_doc_flag == '00':
        return '00'
    return file_names[deal_doc_flag]


def select_by_name(path, name):
    # 文件
    stock_file = open(path + "\\" + name, mode='r', encoding='utf-8')
    doc = {}
    while 1:
        line = stock_file.readline()[:-1]
        if line == '':
            break
        line_j = json.loads(line.replace("\'", "\""))
        doc[line_j['code']] = line_j
    stock_file.close()
    return doc


if __name__ == '__main__':
    print("")
    select_doc()
